package com.krain.ddbb.activity;

import android.content.Intent;
import android.os.Looper;
import android.widget.Button;
import android.widget.TextView;

import com.krain.corelibrary.util.JsonUtil;
import com.krain.ddbb.R;
import com.krain.ddbb.base.BaseActivity;
import com.krain.ddbb.entity.AppApi;
import com.krain.ddbb.entity.PayCharge;
import com.krain.ddbb.entity.UserIssueBean;
import com.orhanobut.logger.Logger;
import com.pingplusplus.android.PaymentActivity;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Class description
 *
 * @author yamin
 * @date 2016/2/15 下午 05:59
 */
@EActivity(R.layout.activity_pay)
public class PayActivity extends BaseActivity {

    @ViewById(R.id.act_order_detail_desc)
    TextView tv_desc;

    @ViewById(R.id.act_order_detail_contact)
    TextView tv_contact;

    @ViewById(R.id.act_order_detail_num)
    TextView tv_num;

    @ViewById(R.id.act_order_detail_price)
    TextView tv_price;

    @ViewById(R.id.act_order_detail_servetime)
    TextView tv_serveTime;

    @ViewById(R.id.act_pay_btn_wechatpay)
    Button btn_wechat;

    @ViewById(R.id.act_pay_btn_alipay)
    Button btn_alipay;
    private final String TYPE_WX = "wx";
    private final String TYPE_ALIPAY = "alipay";
    private final int REQUEST_CODE_PAYMENT = 343;

    @Extra("bean")
    UserIssueBean bean;

    @Override
    protected String getToolbarTitle() {
        return "支付";
    }


    @AfterViews
    void afterViews() {
        setInformation();
    }


    /**
     * 设置传递过来的信息
     */
    void setInformation() {
        tv_desc.setText(getString(R.string.order_need, bean.getDesc()));
        tv_contact.setText(getString(R.string.service_contact, bean.getUsername()));
        tv_num.setText(getString(R.string.order_number, bean.getOrder_num()));
        tv_price.setText(getString(R.string.serve_price, bean.getServer_price()));
        tv_serveTime.setText(getString(R.string.serve_time, bean.getServer_time()));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_CODE_PAYMENT) {
            String result = data.getExtras().getString("pay_result");
            /* 处理返回值
             * "success" - 支付成功
             * "fail"    - 支付失败
             * "cancel"  - 取消支付
             * "invalid" - 支付插件未安装（一般是微信客户端未安装的情况）
             */
            if (result.equals("success")){
                baseUtil.showSnackBar(mContext,"支付成功");
                finish();
            }else {
                String errorMsg = data.getExtras().getString("error_msg"); // 错误信息
                String extraMsg = data.getExtras().getString("extra_msg"); // 错误信息
                Logger.v("errorMsg = "+errorMsg +"extraMsg="+extraMsg);
                baseUtil.showSnackBar(mContext,errorMsg);

            }

        }
    }

    @Background
    void getPayMsg(String type) {
        String payCharge = AppApi.getsInstance(mContext).getPayMsg(app.getmUserinfo()
                .getAccess_token(), String.valueOf(bean.getId()), type);
        if (payCharge != null) {
            goToPayMent(payCharge);
        }

    }

    @UiThread
    void goToPayMent(String payCharge) {
        try {
            JSONObject object = new JSONObject(payCharge);
            String data = object.optString("data");
            Logger.v("test = "+data);
            Intent intent = new Intent(mContext, PaymentActivity.class);
            intent.putExtra(PaymentActivity.EXTRA_CHARGE, data);
            startActivityForResult(intent, REQUEST_CODE_PAYMENT);
        } catch (JSONException e) {
            e.printStackTrace();
            Logger.e("JSONException="+e.toString());
        }



    }


    @Click(R.id.act_pay_btn_wechatpay)
    void wxPay() {
        getPayMsg(TYPE_WX);
    }

    @Click(R.id.act_pay_btn_alipay)
    void aliPay() {
        getPayMsg(TYPE_ALIPAY);
    }
}
